package com.qc.blogsystemplus.controller;

import com.qc.blogsystemplus.bean.ResponseBean;
import com.qc.blogsystemplus.bean.UserBean;
import com.qc.blogsystemplus.entity.User;
import com.qc.blogsystemplus.mapper.UserMapper;
import com.qc.blogsystemplus.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
public class RegisterController {
    @Autowired
    private UserService userService;

    @GetMapping("/veryuser")
    public ResponseBean VeryUser(@RequestParam("username") String username) {
        User user = userService.getUser(username);
        if (user != null){
            return new ResponseBean(200,"找到",user);
        } else {
            return new ResponseBean(200,"未找到",null);
        }
    }

    @PostMapping("/register")
    public ResponseBean Register(@RequestBody UserBean userBean) {
        User userDb = userService.getUser(userBean.getUsername());
        if (userDb == null){
            int re = userService.addUser(userBean);
            if (re != 0) {
                return new ResponseBean(200,"注册成功,请登陆",null);
            } else {
                return new ResponseBean(401,"注册失败",null);
            }
        } else {
            return new ResponseBean(402,"用户名已经存在",null);
        }
    }
}
